package com.jui.material.widgets;

import java.util.ArrayList;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jui.material.R;
import com.jui.material.views.ProgressBar;
import com.jui.material.views.RadioButton;
import com.jui.material.views.RadioButton.OnCheckListener;

public class SingleListDialog extends android.app.Dialog {

  Context context;
  View view;
  View backView;
  String title;
  TextView titleTextView;
  ListView listView;
  private LayoutInflater mLayoutInflater;
  private ArrayList<String> mItemTitle = new ArrayList<String>();
  private int mSelectItemPostion;

  ListAdapter mAdapter;

  private View.OnClickListener mOnClickListener;
  private RadioButton.OnCheckListener mOnCheckListener;
  boolean mCancelable = true;

  public SingleListDialog(Context context, String title, ArrayList<String> list,int selectedPos) {
    super(context, android.R.style.Theme_Translucent);
    this.title = title;
    this.context = context;
    this.mItemTitle = list;
    this.mSelectItemPostion = selectedPos;
    mAdapter = new ListAdapter();
    mLayoutInflater = LayoutInflater.from(context);
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.single_list_dialog);

    view = (RelativeLayout) findViewById(R.id.contentDialog);
    backView = (RelativeLayout) findViewById(R.id.dialog_rootView);
    backView.setOnTouchListener(new OnTouchListener() {

      @Override
      public boolean onTouch(View v, MotionEvent event) {
       if (mCancelable) {
         if (event.getX() < view.getLeft() || event.getX() > view.getRight()
             || event.getY() > view.getBottom() || event.getY() < view.getTop()) {
           dismiss();
         }
      }
        return false;
      }
    });

    this.titleTextView = (TextView) findViewById(R.id.title);
    if (title == null)
      titleTextView.setVisibility(View.GONE);
    else {
      titleTextView.setVisibility(View.VISIBLE);
      titleTextView.setText(title);
    }

    this.listView = (ListView) findViewById(R.id.listView);
    if (mAdapter == null) {
      mAdapter = new ListAdapter();
    }
    listView.setAdapter(mAdapter);
    
    resizeWindow(0);
  }

  public void resizeWindow(int width) {
    WindowManager m = getWindow().getWindowManager();
    Display d = m.getDefaultDisplay();
    Point size = new Point();
    d.getSize(size);
    RelativeLayout.LayoutParams params =
        (android.widget.RelativeLayout.LayoutParams) view.getLayoutParams();
    if (width==0) {
      if (size.x < size.y) {//vertical
        params.width = (int) (size.x * 0.75);
      }else {
        params.width = (int) (size.x * 0.55);
      }
    }else {
      params.width = width;
    }
    params.addRule(RelativeLayout.CENTER_IN_PARENT);
    view.setLayoutParams(params);
  }

  @Override
  public void show() {
    super.show();
    // set dialog enter animations
    view.startAnimation(AnimationUtils.loadAnimation(context, R.anim.dialog_main_show_amination));
    backView.startAnimation(AnimationUtils.loadAnimation(context, R.anim.dialog_root_show_amin));
  }

  @Override
  public void setCancelable(boolean flag) {
    // TODO Auto-generated method stub
    super.setCancelable(flag);
    mCancelable = flag;
  }
  // GETERS & SETTERS

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public TextView getTitleTextView() {
    return titleTextView;
  }

  public void setTitleTextView(TextView titleTextView) {
    this.titleTextView = titleTextView;
  }

  public void setListener(View.OnClickListener mListener) {
    mOnClickListener = mListener;
  }
  
  public void setCheckListener(RadioButton.OnCheckListener mListener){
    mOnCheckListener = mListener;
  }

  public ListAdapter getAdapter() {
    return mAdapter;
  }

  @Override
  public void dismiss() {
    Animation anim = AnimationUtils.loadAnimation(context, R.anim.dialog_main_hide_amination);
    anim.setAnimationListener(new AnimationListener() {

      @Override
      public void onAnimationStart(Animation animation) {}

      @Override
      public void onAnimationRepeat(Animation animation) {}

      @Override
      public void onAnimationEnd(Animation animation) {
        view.post(new Runnable() {
          @Override
          public void run() {
            SingleListDialog.super.dismiss();
          }
        });

      }
    });
    Animation backAnim = AnimationUtils.loadAnimation(context, R.anim.dialog_root_hide_amin);

    view.startAnimation(anim);
    backView.startAnimation(backAnim);
  }

  public class ListAdapter extends BaseAdapter {

    @Override
    public int getCount() {
      // TODO Auto-generated method stub
      return mItemTitle.size();
    }

    @Override
    public Object getItem(int position) {
      // TODO Auto-generated method stub
      return position;
    }

    @Override
    public long getItemId(int position) {
      // TODO Auto-generated method stub
      return position;
    }

    public void setSelectItemPosition(int position) {

      mSelectItemPostion = position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
      TextView textView;
      RadioButton radioButton;
      if (convertView == null) {
          convertView = mLayoutInflater.inflate(R.layout.single_list_item, null);
          textView = (TextView) convertView.findViewById(R.id.md_dialog_single_list_title);
          radioButton = (RadioButton) convertView.findViewById(R.id.md_dialog_single_list_check);
          convertView.setTag(R.id.md_single_list_text,textView);
          convertView.setTag(R.id.md_single_list_check,radioButton);
      }else {
        textView = (TextView) convertView.getTag(R.id.md_single_list_text);
        radioButton = (RadioButton) convertView.getTag(R.id.md_single_list_check);
      }
      convertView.setTag(position);
      convertView.setOnClickListener(mOnClickListener);
      radioButton.setOncheckListener(mOnCheckListener);
      radioButton.setTag(position);
      if (mSelectItemPostion == position) {
        radioButton.setChecked(true);
      }else {
        radioButton.setChecked(false);
      }
      textView.setText(mItemTitle.get(position));
      return convertView;
    }
    

  }

}
